package com.demo.java.spring.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CatalogManagerImpl implements CatalogManager {

    @Autowired
    private CatalogService catalogService;

    @Override
    @Transactional
    public Catalog increUpdate() {
        Catalog catalog = catalogService.findByIdForUpdateSql(1);
        catalog.setBrowseCount(catalog.getBrowseCount() + 1);
        catalogService.updateCatalog(catalog);
        return catalog;
    }

    public Catalog increUpdateNoTx() {
        Catalog catalog = catalogService.findByIdForUpdateSql(1);
        catalog.setBrowseCount(catalog.getBrowseCount() + 1);
        catalogService.updateCatalog(catalog);
        return catalog;
    }

    public Catalog increUpdateNoTxSelect() {
        Catalog catalog = catalogService.findById(1);
        catalog.setBrowseCount(catalog.getBrowseCount() + 1);
        catalogService.updateCatalog(catalog);
        return catalog;
    }

    @Override
    @Transactional
    public Catalog reset() {
        Catalog catalog1 = catalogService.findById(1);
        catalog1.setBrowseCount(0);
        catalogService.updateCatalog(catalog1);
        return catalog1;
    }
}
